package com.chalk.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.chalk.model.News;
import com.chalk.dao.NewsDao;
import com.chalk.service.NewsService;

import com.chalk.util.MapUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author glq
 * @since 2020-12-19
 */

@Service
public class NewsServiceImpl extends ServiceImpl<NewsDao, News> implements NewsService {

    @Override
    public int addNews(News entity) {
        return baseMapper.insert(entity);
    }

    @Override
    public int updateNews(News entity) {
        return baseMapper.updateById(entity);
    }


    @Override
    public int deleteNews(Integer id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public Page<News> getNewsForPage(Map<String, Object> map) {
        Integer page = MapUtil.getPage(map);
        Integer size = MapUtil.getSize(map);
        String newsTypeId = (String) map.get("newsTypeId");
        String newsTitle=(String)map.get("newsTitle");
        //构造器
        EntityWrapper<News> wrapper = new EntityWrapper<>();
        wrapper.eq("is_delete",0);
        if (StringUtils.isNotBlank(newsTypeId)) {
            wrapper.eq("news_type_id", Integer.valueOf(newsTypeId));
        }
        if(StringUtils.isNotBlank(newsTitle)){
            wrapper.like("news_title",newsTitle);
        }
        //排序
        List<String> desc = new ArrayList<>();
        desc.add("create_time");
        Page<News> newsPage = new Page<>(page, size);
        newsPage.setDescs(desc);
        return selectPage(newsPage,wrapper);
    }
}
